<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>天知道</title>
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/index.css" />
  </head>

  <body>
    <div class="wrap" id="app">
      <div class="search_form">
        <div class="logo"><img src="img/logo.png" alt="logo" /></div>
        <div class="form_group">
          <!-- 搜索框 -->
          <input type="text" class="input_txt" placeholder="请输入查询的天气" />
          <!-- 搜索按钮 -->
          <button class="input_sub">搜 索</button>
        </div>
        <!-- 热门城市 -->
        <div class="hotkey">
          <a href="javascript:;">北京</a><a href="javascript:;">上海</a
          ><a href="javascript:;">广州</a><a href="javascript:;">深圳</a>
        </div>
      </div>
      <!-- 搜索结果 -->
      <ul class="weather_list">
        <!-- 列表项 -->
        <li>
          <div class="info_type"><span class="iconfont">小雨</span></div>
          <div class="info_temp">
            <b>低温 4℃</b>
            ~
            <b>高温 13℃</b>
          </div>
          <div class="info_date"><span>3日星期一</span></div>
        </li>
        <li>
          <div class="info_type"><span class="iconfont">中雪</span></div>
          <div class="info_temp">
            <b>低温 2℃</b>
            ~
            <b>高温 6℃</b>
          </div>
          <div class="info_date"><span>4日星期二</span></div>
        </li>
      </ul>
    </div>
    <script src="./node_modules/axios/dist/axios.min.js"></script>
    <script src="./node_modules/dayjs/dayjs.min.js"></script>
    <script>
      async function getWeather(city) {
        const res = await axios({
          url: `http://wthrcdn.etouch.cn/weather_mini?city=${city}`,
        })
        console.log(res)
        if (res.data.desc == "invilad-citykey") {
          alert("城市不存在")
          return
        }
        document.querySelector(".weather_list").innerHTML =
          res.data.data.forecast
            .map((v) => {
              return `<li>
                      <div class="info_type"><span class="iconfont">${v.type}</span></div>
                      <div class="info_temp">
                        <b>${v.low}</b>
                        ~
                        <b>${v.high}</b>
                      </div>
                      <div class="info_date"><span>${v.date}</span></div>
                    </li>`
            })
            .join("")
      }
      window.onload = async function () {
        getWeather("北京")
      }
      try {
        document.querySelector(".input_sub").onclick = async function () {
          const input = document.querySelector(".input_txt").value
          getWeather(input)
        }
      } catch (error) {
        console.dir(error)
      }

      document.querySelector(".input_txt").onkeyup = function (e) {
        if (e.keyCode == "13") {
          const input = document.querySelector(".input_txt").value
          getWeather(input)
        }
      }
      document.querySelector(".hotkey").addEventListener("click", function (e) {
        if (e.target.innerHTML == "上海") {
          getWeather("上海")
        } else if (e.target.innerHTML == "广州") {
          getWeather("广州")
        } else if (e.target.innerHTML == "深圳") {
          getWeather("深圳")
        } else {
          getWeather("北京")
        }
      })
    </script>
  </body>
</html>
